/**
This file is part of Gel2DE.

    Gel2DE is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Gel2DE is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Gel2DE.  If not, see <http://www.gnu.org/licenses/>.

	Copyright (C) 2012, University of Bergen
*/

#pragma once

#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkImageActor.h"
#include "vtkRenderWindow.h"
#include "vtkTIFFReader.h"
#include "vtkLookupTable.h"
#include "vtkImageMapToColors.h"
#include "itkImageToVTKImageFilter.h"
#include "vtkCommand.h"
#include "vtkCallbackCommand.h"
#include "vtkImageGridSource.h"
#include "vtkInteractorStyleUser.h"
#include "vtkCoordinate.h"
#include "vtkCamera.h"
#include "vtkObject.h"
#include "vtkLineSource.h"
#include "vtkPoints.h"
#include "vtkCellArray.h"
#include "vtkPolyDataMapper.h"
#include "vtkProperty.h"
#include "vtkPropPicker.h"
#include "vtkWindowLevelLookupTable.h"
#include "vtkImageMapToColors.h"

#include "GelCorrelationAnalysis.h"
#include "ResultImageViewer.h"

class ResultImageViewer;

class GelImageViewer
{
public:
	typedef itk::ImageToVTKImageFilter<GelImage::ImageType> ConnectorType;

	GelImageViewer(vtkRenderWindowInteractor*);
	~GelImageViewer(void);
	void SelectMovingImage(int);
	void SelectFixedImage(int);
	GelImage* GetGelImage();
	void Render();
	void ResetCamera();
	vtkRenderWindowInteractor* GetInteractor();
	void RegisterEvent(unsigned long);
	void RoiEvent(unsigned long);
	void SetFixedOpacity(double);
	void SetMovingOpacity(double);
	void ShowData();
	void ShowStandard();
	void RegisterMode();
	void ROIMode();
	void ZoomMode();
	vtkCamera* GetCamera();
	void InitializeRectangle();
	void UpdateRectangle(vtkPoints* aPoints);
	void SetRectangleVisible(int aVisible);
	void SetResultImageViewer(ResultImageViewer* aResultImageViewer);
	void SetGelCorrelationAnalysis(GelCorrelationAnalysis* aGelCorrelationAnalysis);
	void Clear();
	void SetWindow(double aWindow);
	void SetLevel(double aLevel);
    void updateRectangle(int* currentPos);
	void GetROIDefinition(GelCorrelationAnalysis::RGBImageType::RegionType* aRoiDefinition);

private:	
	void ConstructVTK();
	void AddRegisterObserver();

	vtkRenderWindowInteractor* m_renderWindowInteractor;
	vtkRenderer* m_renderer;
	vtkImageActor* m_actor;
	vtkImageActor* m_actorFixed;
	ConnectorType::Pointer m_connector;
	ConnectorType::Pointer m_connectorFixed;
	GelImage* m_gelImage;
	GelImage* m_gelImageFixed;
	vtkCallbackCommand* m_vtkCallbackCommand;
	vtkPropPicker* m_pointPicker;
	vtkWindowLevelLookupTable *m_lut;
	vtkImageMapToColors *m_vtkImageMapToColors;
	vtkImageMapToColors* m_vtkImageMapToColorsFixed;
	vtkImageShiftScale *m_shift;
	vtkImageShiftScale *m_shiftFixed;
	bool m_leftButtonPressed;
	bool m_rightButtonPressed;
	int m_pressedPos[2];
	int m_prevPos[2];
	int m_center[2];
	double m_buttonDownPosition[3];
	double m_currentPos[3];

	bool m_setCenterMode;
	bool m_rotateMode;
	bool m_moveMode;

	vtkActor* m_rectangleActor;
	vtkPoints* m_rect;

	GelCorrelationAnalysis* m_gelCorrelationAnalysis;
	ResultImageViewer* m_resultImageViewer;
};
